package club.smartdot.da.controller.student;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import club.smartdot.da.domain.student.Student;
import club.smartdot.da.service.student.StudentService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@RestController
@RequestMapping(value = "/studentController")
public class StudentController {
	
	@Autowired
	private StudentService studentService;
	
	@ApiOperation(value = "持久化student实体", notes = "持久化student实体")
	@ApiImplicitParam(name = "student", value = "学生实体", required = true, dataType="Student")
	@PutMapping(value = "/save")
	public int save(Student student) {
		int affect = studentService.insert(student);
		return affect;
	}
	
	@ApiOperation(value="根据条件获取学生集合并分页", notes="根据条件获取学生集合并分页")
	@ApiImplicitParams({
		 @ApiImplicitParam(name = "student", value = "学生实体", required = false, dataType="Student",paramType="body"),
         @ApiImplicitParam(name = "pageNum", value = "页数", required = true, dataType = "Integer"),
         @ApiImplicitParam(name = "pageSize", value = "记录数", required = true, dataType = "Integer"),
	})
	@GetMapping(value = "/list")
	public List<Student> list(Student student, int pageNum, int pageSize) {
		Page<Student> page = PageHelper.startPage(pageNum, pageSize);
		List<Student> students = studentService.findByCondition(student);
		System.out.println("总共条数："+page.getTotal());
		return students;
	}
	
	@ApiOperation(value="删除学生", notes="根据id删除学生")
	@ApiImplicitParam(name = "id", value = "学生ID", required = true, dataType = "Integer", paramType = "path")
	@DeleteMapping(value = "/remove/{id}")
	public int remove() {
		Student student = new Student();
		student.setName("zhangsan");
		int affect = studentService.deleteByCondition(student);
		return affect;
	}
	
	@ApiOperation(value="根据实体查询数量", notes="根据实体查询数量")
	@GetMapping(value = "/count")
	public long count(@ApiParam(value = "学生实体") Student student) {
		if(student.getName().length() == 0) {
			student.setName(null);
		}
		long record = studentService.findByCount(student);
		return record;
	}
}
